package com.csproj.siiat.base.common.menu.service;


import com.csproj.siiat.base.common.menu.SysMenuDTO;
import com.csproj.siiat.config.Languagesproperties;
import com.csproj.system.domain.Language;
import com.csproj.siiat.base.common.menu.model.Localization;
import com.csproj.siiat.base.common.menu.model.MenuSession;
import com.csproj.siiat.base.common.menu.model.SysAuth;
import com.csproj.siiat.security.SecurityUtils;
import com.csproj.system.domain.AuthorityInfo;
import com.csproj.system.repository.AuthorityInfoRepository;
import com.csproj.system.repository.LanguageRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class SysMenuService {


    private final AuthorityInfoRepository authorityInfoRepository;

    private final LanguageRepository languageRepository;

    private final Languagesproperties languagesproperties;

    public SysMenuService(AuthorityInfoRepository authorityInfoRepository, LanguageRepository languageRepository, Languagesproperties languagesproperties) {
        this.authorityInfoRepository = authorityInfoRepository;
        this.languageRepository = languageRepository;
        this.languagesproperties = languagesproperties;
    }

    /**
     * anth / language控制getAll方法
     *
     * @return
     */
    public SysMenuDTO getAll() {
        //all auth
        List<AuthorityInfo> allPerAuth = authorityInfoRepository.findAll();
        //currentAuth
        List<AuthorityInfo> grantedPermissions = authorityInfoRepository.findByUserInfoId(SecurityUtils.getCurrentUserId());
        Map<String, Boolean> allPerAuthMaps = authInfoToMap(allPerAuth);
        Map<String, Boolean> grantedPermissionMaps = authInfoToMap(grantedPermissions);
        SysAuth sysAuth = new SysAuth(allPerAuthMaps, grantedPermissionMaps);

        //Session
        MenuSession session = new MenuSession();

        //localization
        Localization localization = new Localization();
        // setCurrentCulture
        Map<String,String> currentCulture = new HashMap<>();
        List<Language> languageDefault = languageRepository.findByDefault();
        if(languageDefault.size()>0){
            currentCulture.put("name",languageDefault.get(0).getName());
            currentCulture.put("displayName",languageDefault.get(0).getDisplayName());
        }
        localization.setCurrentCulture(currentCulture);

        List<Language> languages = languageRepository.findAll();
        localization.setLanguages(languages);
        //TODO currentLanguage （根据选择）
        String languageName = "";
        Language currentLanguage = new Language();
        if(languageDefault.size()>0){
            currentLanguage = languageDefault.get(0);
            languageName = currentLanguage.getName();
        }
        localization.setCurrentLanguage(currentLanguage);

        //setsource
        List<Map<String,String>> sources = new ArrayList<>();
        Map<String,String> siiat = new HashMap<>();
        siiat.put("name","Siiat");
        siiat.put("type","MultiTenantLocalizationSource");
        sources.add(siiat);
        localization.setSources(sources);
        //setvalues (WebApp)
        Map<String,Object> en = languagesproperties.getEn();
        Map<String,Object> zh = languagesproperties.getZh();
        Map<String,Object> pu = languagesproperties.getPu();
        Map<String,Object> values = new HashMap<>();
        switch (languageName){
            case "en":
                values.put("Siiat",en);
                break;
            case "zh":
                values.put("Siiat",zh);
                break;
            case "pu":
                values.put("Siiat", pu);
        }
        localization.setValues(values);
        if(localization.getValues()==null){
            localization.setValues(en);
        }
        SysMenuDTO sysMenuDTO = new SysMenuDTO(sysAuth,localization,session);

        return sysMenuDTO;

    }

    private Map<String, Boolean> authInfoToMap(List<AuthorityInfo> authorityInfos) {
        Map<String, Boolean> authmap = new HashMap<>();
        authorityInfos.forEach(a -> {
            String key = "";
//            if (StringUtils.isNotBlank(a.getName())) {
//                key += a.getName();
//            }
            if (StringUtils.isNotBlank(a.getCode())) {
                key += a.getCode();
            }

            authmap.put(key, true);
        });
        return authmap;
    }
}
